    //
//  RegisterReportViewController.m
//  iOsiris-v2
//
//  Created by Admin on 07-01-11.
//  Copyright 2011 Bedrijf. All rights reserved.
//

#import "RegisterReportViewController.h"
#import "Database.h"
#import "Course.h"

@implementation RegisterReportViewController

@synthesize course;
@synthesize aTableView;
@synthesize infoLabel;
@synthesize infoLabelText;
@synthesize user;
@synthesize periodId;


- (IBAction) buttonReturn:(id) sender
{
	[self.view removeFromSuperview];
}

- (void) setUserAccount:(Account *)userAccount
{
	user = userAccount;
}

- (void) getRegisteredCourses
{
	Database *database = [[Database alloc] init];
	
	//int count = [database getOpenRegisterCount:period: 1: user.userid];
	
	//if (count > 0) {
		//int perioddataid = [database getPeriodDataId:year :period];
		course = [database getRegistrations :user.userid];
	//}
	//else {
	//	label.text = @"Currently there are no courses to register to";
	//}
	
	[database closeDatabase];
	[database release];
}

- (void) setNoRegisterdCourses: (NSString *)str
{
	infoLabeltext = str;
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
	//UIView *registerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	UIView *registerReportView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
	//registerView.backgroundColor = [UIColor whiteColor];
	registerReportView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg1.png"]];
	
	UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
	[button setTitle:@"Back" forState:UIControlStateNormal];
	[button addTarget:self action:@selector(buttonReturn:) forControlEvents:UIControlEventTouchDown];
	button.frame = CGRectMake(20.0, 380.0, 80.0, 30.0);
	
	infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 250.0, 300.0, 30.0)];
	infoLabel.font = [UIFont fontWithName:@"C=verdana" size:10];
	infoLabel.textColor = [UIColor redColor];
	infoLabel.text = infoLabeltext;
	
	UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 40.0, 300.0, 20.0)];
	titleLabel.font = [UIFont fontWithName:@"C=verdana" size:12];
	titleLabel.textColor = [UIColor blueColor];
	titleLabel.text = @"Overview registered Courses";
	
	NSLog(@"loadview registerview");
	aTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 70.0, 320.0, 348.0) style:UITableViewStyleGrouped];
	aTableView.backgroundColor = [UIColor whiteColor];
	aTableView.delegate = self;
	aTableView.dataSource = self;
	aTableView.autoresizesSubviews;
	
	[registerReportView addSubview:aTableView];
	[registerReportView addSubview:button];
	[registerReportView addSubview:titleLabel];
	[registerReportView addSubview:infoLabel];
	[self.view addSubview:registerReportView];
}


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
	[course release];
	[aTableView release];
	
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	//switch (section) {
	//case 0:
	return [self.course count];
	//case 1:
	//return 2;
	//}
	
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIndentifier = @"CellIndentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
	
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];
	}
	
	//get the selection object
	Course *aCourse = [self.course objectAtIndex:[indexPath row]];
	
	cell.textLabel.text = aCourse.courseHeader;
	
	return cell;
}


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSInteger row = [indexPath row];
	if(row == -1)
		return nil;
	
	return indexPath;					 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{	
	//Course *selection = [self.course objectAtIndex:[indexPath row]];
	
	//selectController = [[RegisterViewSelectController alloc] initWithNibName:@"RegisterViewSelectController" bundle:nil];
	//[selectController setLabels:selection];	
	//[self.view addSubview:selectController.view];
	
	//[selectController release];
}


@end
